import { message } from 'antd';
import { addDistrict, deleteDistrict, updateDistrict, getDistrictPage } from '../services/api';

export default {
  namespace: 'district',

  state: {
    districtList: [],
  },
  effects: {
    *getDistrictList({ payload }, { call, put }) {
      const response = yield call(getDistrictPage, payload);
      if (response.code === 0) {
        yield put({
          type: 'districtList',
          payload: response,
        });
      }
    },
    *addDistrict({ payload, callback }, { call, put }) {
      const response = yield call(addDistrict, payload);
      if (response.code === 0) {
        message.success('新增成功');
        if (callback) callback();
      }
    },
    *manageCommunity({ payload, callback }, { call, put }) {
      const response = yield call(updateDistrict, payload);
      if (response.code === 0) {
        message.success('修改成功')
        if (callback) callback();
      }
    },
    *delDistrict({ payload, callback }, { call, put }) {
      const response = yield call(deleteDistrict, payload);
      if (response.code === 0) {
        message.success('删除成功')
        if (callback) callback();
      }
    },
  },
  reducers: {
    districtList(state, action) {
      return {
        ...state,
        districtList: action.payload.data,
      };
    },
  },
};
